<?php
/**
 *  Generic "Single" Model: the single-row table access
 *
 *
 */
namespace Yz\modules\Generic;

class Model_Single
    extends Model_Abstract
    implements \Yz_Model_Interface
{

    /**
     *  @var Yz_Action_Fetch_Interface
     */
    protected $fetchAction = "fetch";
    /**
     *  @var Yz_Action_Update_Interface
     */
    protected $updateAction = "update";
    /**
     *  @var Yz_Action_Create_Interface
     */
    protected $createAction = "create";
    /**
     *  @var Yz_Action_Delete_Interface
     */
    protected $deleteAction = "delete";
    /**
     *  a name of item class (Yz_Item_Interface)
     *  @var string
     */
    protected $itemName = "default";


    /************************************
     *          Public methods          *
     ************************************/


    /**
     *  fetch an item from database
     *  @return Yz_Item_Interface/Item_Default
     *  @throw Yz_Exception_ItemNotFound
     */
    public function fetchItem()
    {
        debug();

        $item = $this->selectItem();

        $action = $this->selectAction($this->fetchAction);

        $values = $this->_exec($action)->fetch();

        return $item->assignValues($values);
    }


    /**
     *  updateItem
     *
     */
    public function updateItem(\Yz_Item_Interface $item)
    {
        debug();
        $action = $this->selectAction("update");
        $action->setValues($item);
        $this->_exec($action); // returns action (ignored) or throws exception
        return $this;
    }

    /**
     *
     *  @todo The single row may not be created ???
     */
    public function createItem()
    {
        debug();
        throw new \Yz_Exception("The single row must exist");
    }

    /**
     *
     *  @todo The single row may not be deleted ???
     *
     */
    public function deleteItem($item)
    {
        debug();
        throw new \Yz_Exception("The single row may not be deleted");
    }

}
